/*
 * @Description: mark
 * @Author: wangwangwang
 * @Date: 2020-11-19 20:26:32
 * @LastEditors: wangwangwang
 * @LastEditTime: 2021-02-24 11:50:31
 */
import axios from 'axios'
import {
  Message
} from 'element-ui'
import {
  getToken,
  getUserId
} from '@/utils/auth';
import router from './router'

// 创建axios实例
// let baseURL = 'http://10.31.6.43:10001'
// let baseURL = 'http://10.31.5.65'
// let baseURL = 'http://10.31.5.66'
// let baseURL = 'http://10.31.6.130:9001'
// let baseURL = 'http://10.31.6.132:9001'
// let baseURL = '/dev'
const service = axios.create({
  baseURL: '',
  timeout: 16000, // 请求超时时间
})

// request拦截器
service.interceptors.request.use(config => {
  config.headers['Content-Type'] = 'application/json'
  if (getToken()) {
    config.headers['access-token'] = getToken()
    config.headers['user-id'] = getUserId()
  }
  return config
}, error => {
  Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(response => {
  const {
    status,
    data
  } = response;
  if (status !== 200) {
    Message('网络出错！！！')
    return
  }
  if (data.code === 1011) {
    Message({
      message: '登录状态失效，请重新登录！！！',
      type: 'warning',
      duration: 1000,
      onClose() {
        router.push({ name: 'login' });
      }
    });
  } else if (data.code !== 1001 && data.code !== 1032) {
    Message({
      showClose: true,
      message: data.codeMsg,
      type: 'warning'
    })
    return data
  } else if (data.code == 1032) {
    Message({
      showClose: true,
      message: data.codeMsg,
      type: 'warning'
    })
    return data
  }
  else {
    return data
  }
}, error => {
  console.log('err' + error) // for debug
  Message({
    showClose: true,
    message: error,
    type: 'warning'
  })
  return Promise.reject(error)
})
export default service;